Find the value of y
according to the following condition:
Input. One integer x (-104 ≤ x ≤ 104).
Output. Print the value of y
according to the given condition.
Sample input 1 |
Sample output 1 |
2 |
4 |
|
|
Sample input 2 |
Sample output 2 |
20 |
8100 |
conditional
statement
Use a
conditional statement to solve the problem. Since x ≤ 10000 = 104, then x3 ≤ 1012. Let’s use the long long type to avoid overflow.
Algorithm
implementation
scanf("%lld",&x);
Compute the value of y.
if (x >= 10)
y = x * x * x + 5 * x;
else
y = x * x – 2 * x + 4;
Print the result.
printf("%lld\n",y);
Algorithm
implementation – ternary
operator
Read the input value of x.
scanf("%lld",&x);
Compute the value of y.
y = (x >= 10) ? x * x * x + 5 * x
: x * x – 2 * x + 4;
Print the result.
printf("%lld\n",y);
Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long y, x = con.nextLong();
if (x >= 10)
y = x*x*x + 5*x;
else
y = x*x - 2*x + 4;
System.out.println(y);
con.close();
}
}
Python implementation
Read the input value of x.
x = int(input())
Compute the value of y.
if x >= 10:
y = x * x * x + 5 * x
else:
y = x * x - 2 * x + 4
Print the result.
print(y)